(function() {
  var _container = document.getElementById('gs');

  document.getElementById('gs_l').onclick = function() {
    woody.dom.hide(_container);
    deberc.gameRoom.leave();
  };

  document.getElementById('gs_s').onclick = function() {
    woody.dom.hide(_container);
    deberc.gameRoom.move('s');
  };

  var _addCallback = function(i, id) {
    vk.api.getUserInfo(id, this, function(info) {
      document.getElementById('gs_' + i).innerHTML =
        '<a href="' + info.url
          + '"><img src="' + info.picture
          + '" class="i" /></a><a href="' + info.url
          + '" class="link">' + info.name + '</a>';
    })
  };

  deberc.gameStarter = {
    hide: function () {
      woody.dom.hide(_container);
    },
    render: function(data) {
      woody.dom.show(_container);
      var ids = data[1].split(',');
      deberc.gameRoom.setPlayers(ids);
      var allPresent = true;
      for (var i in ids) {
        var id = ids[i];
        if (id === '0') {
          document.getElementById('gs_' + i).innerHTML =
            '<a><img class="i" /></a><span class="lgray">Свободно</span>';
          allPresent = false;
        }
        else {
          document.getElementById('gs_' + i).innerHTML =
            '<a><img class="i" /></a><span class="gray">'
              + (deberc.rest.session.isCurrent(ids[i])
              ? 'Вы'
              : 'Соперник') + '</span>';
          _addCallback.call(this, i, id);
        }
      }
      if ((allPresent) && (deberc.rest.session.isCurrent(ids[data[2]]))) {
        woody.dom.visible(document.getElementById('gs_s'));
      }
      else {
        woody.dom.invisible(document.getElementById('gs_s'));
      }
    }
  };
})();
